'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

exports.default = findOne;

var _helpers = require('./helpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function findOne(model, tc, opts) {
  if (!model || !model.modelName || !model.schema) {
    throw new Error('First arg for Resolver findOne() should be instance of Mongoose Model.');
  }

  if (!tc || tc.constructor.name !== 'TypeComposer') {
    throw new Error('Second arg for Resolver findOne() should be instance of TypeComposer.');
  }

  return new tc.constructor.schemaComposer.Resolver({
    type: tc.getType(),
    name: 'findOne',
    kind: 'query',
    args: (0, _extends3.default)({}, (0, _helpers.filterHelperArgs)(tc, model, (0, _extends3.default)({
      filterTypeName: `FilterFindOne${tc.getTypeName()}Input`,
      model
    }, opts && opts.filter)), (0, _helpers.skipHelperArgs)(), (0, _helpers.sortHelperArgs)(tc, model, (0, _extends3.default)({
      sortTypeName: `SortFindOne${tc.getTypeName()}Input`
    }, opts && opts.sort))),
    resolve: resolveParams => {
      resolveParams.query = model.findOne({}); // eslint-disable-line
      (0, _helpers.filterHelper)(resolveParams);
      (0, _helpers.skipHelper)(resolveParams);
      (0, _helpers.sortHelper)(resolveParams);
      (0, _helpers.projectionHelper)(resolveParams);

      return resolveParams.query.exec();
    }
  });
}
/* eslint-disable no-param-reassign */